package xvalidate

import (
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
)

/* ======================================================================================== *
 * 功能:自定义翻译器
 * 描述:这里编写自定义的规则
 * 参考:https://github.com/go-playground/validator/blob/v9/_examples/translations/main.go
 * ======================================================================================== */
func ITranslator(v *validator.Validate, trans ut.Translator) {
	/* ====================================================================== *
	 * 描述:验证 int 类型的数据
	 * ====================================================================== */
	v.RegisterTranslation("x_num", trans,
		func(ut ut.Translator) error {
			return ut.Add("x_num", "{0}参数错误", true)
		},
		func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("x_num", fe.Field())
			return t
		})

	/* ====================================================================== *
	 * 描述:验证数据表 唯一
	 * ====================================================================== */
	v.RegisterTranslation(
		"unique",
		trans,
		func(ut ut.Translator) error {
			return ut.Add("unique", "{0}已存在", true)
		},
		func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("unique", fe.Field())
			return t
		})

	// 从第三方 拷贝过来
	v.RegisterTranslation("valid_username", trans, func(ut ut.Translator) error {
		return ut.Add("valid_username", "{0} 填写不正确哦", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("valid_username", fe.Field())
		return t
	})
	v.RegisterTranslation("valid_service_name", trans, func(ut ut.Translator) error {
		return ut.Add("valid_service_name", "{0} 不符合输入格式", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("valid_service_name", fe.Field())
		return t
	})
	v.RegisterTranslation("valid_rule", trans, func(ut ut.Translator) error {
		return ut.Add("valid_rule", "{0} 必须是非空字符", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("valid_rule", fe.Field())
		return t
	})
	v.RegisterTranslation("valid_url_rewrite", trans, func(ut ut.Translator) error {
		return ut.Add("valid_url_rewrite", "{0} 不符合输入格式", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("valid_url_rewrite", fe.Field())
		return t
	})
	v.RegisterTranslation("valid_header_transfor", trans, func(ut ut.Translator) error {
		return ut.Add("valid_header_transfor", "{0} 不符合输入格式", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("valid_header_transfor", fe.Field())
		return t
	})
	v.RegisterTranslation("valid_ipportlist", trans, func(ut ut.Translator) error {
		return ut.Add("valid_ipportlist", "{0} 不符合输入格式", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("valid_ipportlist", fe.Field())
		return t
	})
	v.RegisterTranslation("valid_iplist", trans, func(ut ut.Translator) error {
		return ut.Add("valid_iplist", "{0} 不符合输入格式", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("valid_iplist", fe.Field())
		return t
	})
	v.RegisterTranslation("valid_weightlist", trans, func(ut ut.Translator) error {
		return ut.Add("valid_weightlist", "{0} 不符合输入格式", true)
	}, func(ut ut.Translator, fe validator.FieldError) string {
		t, _ := ut.T("valid_weightlist", fe.Field())
		return t
	})

}
